package com.lzb.randomturntable.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.basic.utils.SPUtil;
import com.lzb.randomturntable.BaseNoModelFragment;
import com.lzb.randomturntable.Content;
import com.lzb.randomturntable.R;
import com.lzb.randomturntable.adapter.BombAdapter;
import com.lzb.randomturntable.bean.BombBean;
import com.lzb.randomturntable.databinding.FragmentBombBinding;
import com.lzb.randomturntable.dialog.BombDialog;
import com.lzb.randomturntable.dialog.BombSettingDialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BombFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BombFragment extends BaseNoModelFragment<FragmentBombBinding> implements View.OnClickListener, OnItemClickListener {

    private BombAdapter bombAdapter;
    private int remainingBombs = 0; // 剩余炸弹数量

    @Override
    protected int onCreate() {
        return R.layout.fragment_bomb;
    }

    @Override
    protected void initView() {
        mDataBinding.ivBombSetting.setOnClickListener(this);

        mDataBinding.rvBombList.setLayoutManager(new GridLayoutManager(mContext, 5));
        bombAdapter = new BombAdapter();
        mDataBinding.rvBombList.setAdapter(bombAdapter);
        bombAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivBombSetting) {//炸弹设置
            showBombSetting();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        bombAdapter.getItem(position).setShow(false);
        if (bombAdapter.getItem(position).isBomb()) {
            // 减少剩余炸弹数量
            remainingBombs--;
            
            // 检查是否是最后一个炸弹
            boolean isLastBomb = (remainingBombs == 0);
            showBombDialog(isLastBomb);
        }
        bombAdapter.notifyItemChanged(position);
    }

    @Override
    protected void initData() {
        generateBombList();
    }

    private void generateBombList() {
        // 从SP获取配置
        int giftRow = SPUtil.getInt(mContext, Content.giftRow, 5); // 默认5排
        int bombNumber = SPUtil.getInt(mContext, Content.bombNumber, 3); // 默认3个炸弹
        
        // 计算总礼盒数（每排5个）
        int totalGifts = giftRow * 5;
        
        // 创建炸弹位置索引并随机选择
        List<Integer> allPositions = new ArrayList<>();
        for (int i = 0; i < totalGifts; i++) {
            allPositions.add(i);
        }
        Collections.shuffle(allPositions);
        
        // 获取前bombNumber个位置作为炸弹位置
        List<Integer> bombPositions = allPositions.subList(0, Math.min(bombNumber, totalGifts));
        
        // 初始化剩余炸弹数量
        remainingBombs = bombPositions.size();
        
        // 创建BombBean列表
        List<BombBean> bombList = new ArrayList<>();
        for (int i = 0; i < totalGifts; i++) {
            // 检查当前位置是否是炸弹位置
            boolean isBomb = bombPositions.contains(i);
            // 使用礼盒图片，所有盒子都显示礼盒外观
            BombBean bombBean = new BombBean(R.drawable.lihe, isBomb, true);
            bombList.add(bombBean);
        }
        
        // 设置适配器数据
        bombAdapter.setList(bombList);
    }

    private void showBombSetting() {
        BombSettingDialog dialog = new BombSettingDialog(mContext);
        dialog.setListener(new BombSettingDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                // 设置改变后重新生成炸弹列表
                generateBombList();
            }
        });
        dialog.show();
    }

    private void showBombDialog(boolean isLastBomb) {
        BombDialog dialog = new BombDialog(mContext);
        
        // 如果是最后一个炸弹，设置按钮文字为"再来一次"
        if (isLastBomb) {
            dialog.setConfirmText(getString(R.string.play_again));
            dialog.setListener(new BombDialog.Ilistener() {
                @Override
                public void clickConfirm() {
                    // 刷新礼盒列表
                    generateBombList();
                }
            });
        }
        
        dialog.show();
    }

}